package org.ycx.upms.web.controller;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.ycx.common.constants.RespCode;
import org.ycx.common.dto.RespMSG;
import org.ycx.upms.dao.model.SecAttachment;
import org.ycx.upms.dao.model.SecAttachmentExample;
import org.ycx.upms.dao.model.SecUserExample;
import org.ycx.upms.service.ISecAttachmentService;
import org.ycx.upms.service.ISecUserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SecAttachmentcontroller
 * Created by yang on 2017-07-19 16:25:39.
 */
@Controller
@RequestMapping("/sec/attachment")
public class SecAttachmentController {

    private static Logger _log = LoggerFactory.getLogger(SecAttachmentController.class);

    private static final String PRE = "sec/attachment/";

    @Autowired
    private ISecAttachmentService service;

    @Autowired
    private ISecUserService userService;

    /**
     * 首页
     *
     * @param modelMap
     * @return
     */
    @RequestMapping("/index.html")
    public String indexPage(ModelMap modelMap) {
        return PRE + "index";
    }

    @RequestMapping("/users.html")
    public String userPage(Integer fid, ModelMap modelMap) {
        modelMap.put("fid", fid);
        return PRE + "users";
    }

    @RequestMapping("/users")
    @ResponseBody
    public RespMSG grantUsers(Integer fid, @RequestParam("uid") String loginName) {
        RespMSG msg = new RespMSG(RespCode.SUCCESS);
        int res = this.service.addFileUsers(fid, loginName);
        return msg;
    }

    @RequestMapping(value = "/check")
    @ResponseBody
    public boolean check(String uid) {
        SecUserExample sue = new SecUserExample();
        sue.createCriteria().andLoginNameEqualTo(uid);
        int count = this.userService.countByExample(sue);
        if (count <= 0) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.POST)
    @ResponseBody
    public RespMSG delete(@PathVariable("ids") String ids) {
        int count = service.deleteByPrimaryKeys(ids);
        return new RespMSG(RespCode.SUCCESS);
    }


    @RequestMapping("/list")
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        SecAttachmentExample beanExample = new SecAttachmentExample();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            beanExample.setOrderByClause(sort + " " + order);
        }
        //TODO 模糊查询定义
        List<SecAttachment> rows = service.selectByExampleForOffsetPage(beanExample, offset, limit);
        long total = service.countByExample(beanExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }
}
